package com.yangaobo.mapper;

import com.yangaobo.dto.AssetsBorrowPageDTO;
import com.yangaobo.entity.AssetsBorrow;
import org.apache.ibatis.annotations.*;
import org.springframework.stereotype.Repository;

import java.util.List;

/**
 * @author 杨奥博
 */
@Repository
public interface AssetsBorrowMapper {
    @Insert("""
            insert into rms_assets_borrow(`fk_assets_id`, `fk_emp_id`, `count`, `borrow_time`, `expected_return_time`, `return_time`, `info`, `version`, `deleted`, `created`, `updated`)
            values (#{fkAssetsId}, #{fkEmpId}, #{count}, #{borrowTime},#{expectedReturnTime},#{returnTime},#{info},#{version},#{deleted},#{created},#{updated})
            """)
    int insert(AssetsBorrow assetsBorrow);

    @Results(
            id = "assetsBorrowResultMap",
            value = {
                    @Result(property = "id",column = "id",id = true),
                    @Result(property = "fkAssetsId",column = "fk_assets_id"),
                    @Result(property = "fkEmpId",column = "fk_emp_id"),
                    @Result(property = "assets",column = "fk_assets_id",one = @One(select = "com.yangaobo.mapper.AssetsMapper.select")),
                    @Result(property = "emp",column = "fk_emp_id",one = @One(select = "com.yangaobo.mapper.EmpMapper.select"))
            }
    )
    @Select("""
            select * from rms_assets_borrow t where t.id = #{param1} and t.deleted = 0
            """)
    AssetsBorrow select(Long id);

    @ResultMap("assetsBorrowResultMap")
    @Select("""
            <script>
            select * from rms_assets_borrow t
            <where>
                <if test='fkAssetsId != null'> fk_assets_id = #{fkAssetsId} and </if>
                <if test='fkEmpId != null'> fk_emp_id = #{fkEmpId} and </if>
                t.deleted = 0
            </where>
            </script>
            """)
    List<AssetsBorrow> list(AssetsBorrowPageDTO dto);

    @Update("""
            <script>
            update rms_assets_borrow
            <set>
                <if test="fkAssetsId != null">fk_assets_id = #{fkAssetsId},</if>
                <if test="fkEmpId != null">fk_emp_id = #{fkEmpId},</if>
                <if test="count != null">count = #{count},</if>
                <if test="borrowTime != null">borrow_time = #{borrowTime},</if>
                <if test="expectedReturnTime != null">expected_return_time = #{expectedReturnTime},</if>
                <if test="returnTime != null">return_time = #{returnTime},</if>
                <if test="info != null">info = #{info},</if>
                <if test="deleted != null">deleted = #{deleted},</if>
                <if test="created != null">created = #{created},</if>
                <if test="updated != null">updated = #{updated},</if>
                version=version+1
            </set>
            where id = #{id} and deleted = 0 and version = #{version}
            </script>
            """
            )
    int update(AssetsBorrow assetsBorrow);

    @Update("""
            update rms_assets_borrow set deleted = 1, updated = current_timestamp where id = #{id}
            """)
    int delete(Long id);

    @Update("""
            <script>
            update rms_assets_borrow set deleted = 1, updated = current_timestamp where
            <foreach collection='list' item='e' open='id in (' close= ')' separator=','>
                #{e}
            </foreach>
            </script>
            """)
    int deleteBatch(List<Long> ids);

}
